#include "stdafx.h"
#include "SoundEventParameterDialog.h"

IMPLEMENT_DYNAMIC(CSoundEventParameterDialog, CDialog)

BEGIN_MESSAGE_MAP(CSoundEventParameterDialog, CDialog)
		ON_WM_HSCROLL()
		ON_WM_SIZE()
END_MESSAGE_MAP()

//////////////////////////////////////////////////////////////////////////
CSoundEventParameterDialog::CSoundEventParameterDialog(CWnd* pParent /*=NULL*/)
:	CDialog(CSoundEventParameterDialog::IDD, pParent),
m_nSoundID(INVALID_SOUNDID),
m_nIndex(-1),
m_eType(spNONE),
m_fSliderWidthMultiplier(1.0f)
{
}

//////////////////////////////////////////////////////////////////////////
CSoundEventParameterDialog::~CSoundEventParameterDialog()
{
		DestroyWindow();
}

//////////////////////////////////////////////////////////////////////////
void CSoundEventParameterDialog::DoDataExchange(CDataExchange* pDX)
{
		CDialog::DoDataExchange(pDX);
}

//////////////////////////////////////////////////////////////////////////
BOOL CSoundEventParameterDialog::OnInitDialog()
{
		// Arrange the children right after dialog creation
		ArrangeChildren();

		return TRUE;
}

//////////////////////////////////////////////////////////////////////////
void CSoundEventParameterDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
		CSliderCtrl const* const pSlider	= (CSliderCtrl*)pScrollBar;
		float const fValue								= static_cast<float>(pSlider->GetPos())*0.01f; // Can't use "nPos", on button-up on the slider nPos always gets 0
		CWnd* const pCurrentValue					= GetDlgItem(IDC_SOUND_PARAMETER_CURR);

		if(pCurrentValue)
		{
				char cTemp[16] = {'\0'};
				sprintf_s(cTemp, 16, "[%.2f]", fValue);
				pCurrentValue->SetWindowText(cTemp);
		}

		// Set value of this slider (if user set both index and type index will be preferred atm)
		if (m_nSoundID != INVALID_SOUNDID)
		{
				_smart_ptr<ISound> const pSound = gEnv->pSoundSystem->GetSound(m_nSoundID);

				if (pSound != NULL)
				{
						if (m_nIndex > -1)
						{
								pSound->SetParam(m_nIndex, fValue);
						}
						else if (m_eType != spNONE)
						{
								ptParamF32 ParamF32(fValue);
								pSound->SetParam(m_eType, &ParamF32);
						}
				}
		}

		CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

//////////////////////////////////////////////////////////////////////////
void CSoundEventParameterDialog::SetName(char const* const pcName)
{
		CWnd* const pParameterName = GetDlgItem(IDC_SOUND_PARAMETER_NAME);
		if(pParameterName)
				pParameterName->SetWindowText(pcName);

		m_sParameterName = pcName;
}

//////////////////////////////////////////////////////////////////////////
void CSoundEventParameterDialog::SetCurrentValue(float const fCurrentValue)
{
		CSliderCtrl* const pSlider = (CSliderCtrl*)GetDlgItem(IDC_SOUND_PARAMETER_SLIDER);
		if(pSlider)
				pSlider->SetPos(static_cast<int>(fCurrentValue*100.0f));

		CWnd* const pCurrentValue	= GetDlgItem(IDC_SOUND_PARAMETER_CURR);
		if(pCurrentValue)
		{
				char cTemp[16] = {'\0'};
				sprintf_s(cTemp,16,"[%.2f]",fCurrentValue);
				pCurrentValue->SetWindowText(cTemp);
		}
}

//////////////////////////////////////////////////////////////////////////
float CSoundEventParameterDialog::GetCurrentValue() const
{
		CSliderCtrl const* const pSlider = (CSliderCtrl*)GetDlgItem(IDC_SOUND_PARAMETER_SLIDER);
		if(pSlider)
				return static_cast<float>(pSlider->GetPos())*0.01f;

		return 0.0f;
}

//////////////////////////////////////////////////////////////////////////
void CSoundEventParameterDialog::SetRange(float const fRangeMin, float const fRangeMax)
{
		CSliderCtrl* const pSlider = (CSliderCtrl*)GetDlgItem(IDC_SOUND_PARAMETER_SLIDER);

		if(CWnd* const pRangeMin = GetDlgItem(IDC_SOUND_PARAMETER_MIN))
		{
				char cTemp[16] = {'\0'};
				sprintf_s(cTemp,16,"%.2f",fRangeMin);
				pRangeMin->SetWindowText(cTemp);

				if(pSlider)
						pSlider->SetRangeMin(static_cast<int>(fRangeMin*100.0f));
		}

		if(CWnd* const pRangeMax = GetDlgItem(IDC_SOUND_PARAMETER_MAX))
		{
				char cTemp[16] = {'\0'};
				sprintf_s(cTemp,16,"%.2f",fRangeMax);
				pRangeMax->SetWindowText(cTemp);

				if(pSlider)
						pSlider->SetRangeMax(static_cast<int>(fRangeMax*100.0f));
		}
}

//////////////////////////////////////////////////////////////////////////
void CSoundEventParameterDialog::SetSoundID(tSoundID const nSoundID)
{
		m_nSoundID = nSoundID;

		if (m_nSoundID != INVALID_SOUNDID)
		{
				_smart_ptr<ISound> const pSound = gEnv->pSoundSystem->GetSound(m_nSoundID);

				if (pSound != NULL)
				{
						// Now update our parameter
						CSliderCtrl const* const pSlider = (CSliderCtrl*)GetDlgItem(IDC_SOUND_PARAMETER_SLIDER);

						if (pSlider)
						{
								float const fValue = static_cast<float>(pSlider->GetPos())*0.01f;

								if (m_nIndex > -1)
								{
										pSound->SetParam(m_nIndex, fValue);
								}
						}
				}
		}
}

//////////////////////////////////////////////////////////////////////////
void CSoundEventParameterDialog::ArrangeChildren()
{
		// If we get resized make sure to resize our child items
		CWnd* const pParameterName	= GetDlgItem( IDC_SOUND_PARAMETER_NAME );
		CWnd* const pSlider					= GetDlgItem( IDC_SOUND_PARAMETER_SLIDER );
		CWnd* const pCurrentValue		= GetDlgItem( IDC_SOUND_PARAMETER_CURR );
		CWnd* const pRangeMin				= GetDlgItem( IDC_SOUND_PARAMETER_MIN );
		CWnd* const pRangeMax				= GetDlgItem( IDC_SOUND_PARAMETER_MAX );

		// Reposition the slider to center Y
		if (pSlider)
		{
				CRect oRect;
				GetClientRect( oRect );
				int const nDialogWidth			= oRect.Width();
				int const nDialogCenter[2]	= { oRect.Width() / 2, oRect.Height() / 2 };

				pSlider->GetClientRect( oRect );
				int const nSliderHeight		= oRect.Height();
				int const nSliderNewWidth	= static_cast<int>(static_cast<float>(nDialogWidth) * m_fSliderWidthMultiplier);

				pSlider->SetWindowPos( NULL, nDialogCenter[0] - nSliderNewWidth / 2, nDialogCenter[1] - nSliderHeight / 2, nSliderNewWidth, nSliderHeight, 0);

				// Reposition + resize parameter text field
				if (pParameterName)
				{
						pParameterName->GetClientRect( oRect );
						pParameterName->SetWindowPos( NULL, 0, (nDialogCenter[1] - nSliderHeight / 2) - oRect.Height(), nDialogWidth, oRect.Height(), 0 );
				}

				// Reposition + resize current value text field
				if (pCurrentValue)
				{
						pCurrentValue->GetClientRect( oRect );
						pCurrentValue->SetWindowPos( NULL, 0, nDialogCenter[1] + nSliderHeight / 2, nDialogWidth, oRect.Height(), 0 );
				}

				// Reposition range min text field
				if (pRangeMin)
				{
						pRangeMin->GetClientRect( oRect );

						// Position it 10 pixel lower than the current value text field
						pRangeMin->SetWindowPos( NULL, nDialogCenter[0] - nSliderNewWidth / 2, (nDialogCenter[1] + nSliderHeight / 2) + 10, oRect.Width(), oRect.Height(), 0 );
				}

				// Reposition range max text field
				if (pRangeMax)
				{
						pRangeMax->GetClientRect( oRect );

						// Position it 10 pixel lower than the current value text field
						pRangeMax->SetWindowPos( NULL, (nDialogCenter[0] + nSliderNewWidth / 2) - oRect.Width(), (nDialogCenter[1] + nSliderHeight / 2) + 10, oRect.Width(), oRect.Height(), 0 );
				}
		}
}

//////////////////////////////////////////////////////////////////////////
void CSoundEventParameterDialog::OnSize(UINT nType, int cx, int cy)
{
		ArrangeChildren();
}